const JobService = require('../../service/job')
const Response = require('../../utils/response')

async function newJob(ctx) {
    ctx.request.body.status = 0
    const response = new Response()
    const result = await JobService.newJob({
        ctx
    })
    if (result.result) {
        response.SUCCESS = 200
        response.DATA = result
    } else {
        response.FAIL = 500
        response.DATA = '错误'
    }
    ctx.body = response.getData()
}

async function getAllJob(ctx) {
    const result = await JobService.getAllJob({ctx})
    const response = new Response()
    response.SUCCESS = 200
    response.DATA = {
        count: result.length,
        Jobs: result
    }
    ctx.body = response.getData()
}

async function getJobById(ctx) {
    const { id } = ctx.params
    const { job } = await JobService.getJobById({ ctx, id })
    const response = new Response()

    if(job) {
      response.SUCCESS = 200
      response.DATA = job
    }else{
      response.FAIL = 500
      response.DATA = '记录不存在'
    }
    ctx.body = response.getData()
}


async function delJob(ctx) {
    const {
        id
    } = ctx.request.body

    const result = await JobService.delJob({
        id
    })

    const response = new Response()

    if (result) {
        response.SUCCESS = 200
        response.DATA = '删除成功'
    } else {
        response.FAIL = 500
        response.DATA = '删除错误'
    }
    ctx.body = response.getData()
}

async function setJob(ctx) {
    const {
        id
    } = ctx.request.body
    const result = await JobService.setJob({
        ctx,
        id
    })
    const response = new Response()
    if (result) {
        response.SUCCESS = 200
        response.DATA = '修改成功'
    } else {
        response.FAIL = 500
        response.DATA = '错误'
    }
    ctx.body = response.getData()
}

module.exports = {
    newJob,
    getAllJob,
    delJob,
    setJob,
    getJobById
}